/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.solution;

import java.io.Serializable;

import br.ufmg.dees.insane.util.IVector;


/** 
 * A class defining a
 *
 * @author Fuina, Jamile & Pitangueira, Roque
 * @since July 2005
 */
/**
 * @author jamile
 *
 */
public class DisplacementControl implements IteractiveStrategy, Serializable{
    
	private static final long serialVersionUID = 1L;    
	
    private Step step;
    private double loadFactor;
    private String nodeControl;		
    private int directionControl;	
    private int eqCtrl;
    private double lambda;
    
    public DisplacementControl() {
    }

    /**
     * @param loadFactor the displacement adopted to the Displacement Control Strategy
     */
    public DisplacementControl(double loadFactor, String nodeControl, int directionControl) {
		this.loadFactor = loadFactor;			
		this.nodeControl = nodeControl;
		this.directionControl = directionControl;
		
	}  
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#getPredictor()
     */
    public double getPredictor(){
        eqCtrl = this.step.getAssembler().getModel().getNode(nodeControl).getNodeValues(this.step.getAssembler().getModel().getNode(nodeControl).EQUATIONS).getIntPointValue(directionControl-1);
//        int sizeOfXu = this.step.getAssembler().getSizeOfXu();
//        IVector x_i_1 = new IVector (sizeOfXu);
//        IVector correct = new IVector (sizeOfXu);
        IVector xp = this.step.getXP();
//    	if(this.step.getLabel() != 1){
//    		IVector x_i = new IVector (sizeOfXu);
//    		IVector aux1 = new IVector (sizeOfXu);
//    		int eqMax = 1;
//       		x_i = this.step.getAssembler().getXu(); 
//        	aux1.sub(x_i, x_i_1);
//        	for ( int j = 0; j < sizeOfXu; j++ )
//        		aux1.setElement(j,Math.abs(aux1.getElement(j)));
//        	correct.add(correct, aux1);
//        	x_i_1 = x_i; 
//            
//    		for (int i=1; i< sizeOfXu; i++) {
//				if (Math.abs(xp.getElement(i)) > Math.abs(xp.getElement(eqMax-1)))
//					eqMax = i+1; 
//    		}
//    		
//    		if(eqMax != eqCtrl){
//    			double dir = xp.getElement(eqMax-1) * xp.getElement(eqCtrl-1);
//    			if (dir > 0.0) dir = 1.0;
//    			else dir = -1.0;
//    			loadFactor *= dir * correct.getElement(eqMax-1) / correct.getElement(eqCtrl-1);
//    			eqCtrl = eqMax;
//    		}
//    			
//    	}
    	lambda = loadFactor / xp.getElement(eqCtrl-1);
    	return lambda;			
    }	

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#getCorrector()
     */
    public double getCorrector(){
        eqCtrl = this.step.getAssembler().getModel().getNode(nodeControl).getNodeValues(this.step.getAssembler().getModel().getNode(nodeControl).EQUATIONS).getIntPointValue(directionControl-1);
    	lambda = - this.step.getUnbalancedX().getElement(eqCtrl-1) / this.step.getXP().getElement(eqCtrl-1);
    	return lambda;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#getLoadFactor()
     */
    public double getLoadFactor() {
        return loadFactor;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#setLoadFactor(double)
     */
    public void setLoadFactor(double lf){
    	this.loadFactor = lf;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#getLambda()
     */
    public double getLambda() {
        return lambda;
    }
    
    /**
	 * @return Returns the nodeControl.
	 */
	public String getNodeControl() {
		return nodeControl;
	}
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#setNodeControl(String)
     */
    public void setNodeControl(String no){
    	this.nodeControl = no;
    }
    
	/**
	 * @return Returns the directionControl.
	 */
	public int getDirectionControl() {
		return directionControl;
	}
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#setDirectionControl(int)
     */
    public void setDirectionControl(int dir){
    	this.directionControl = dir;
    }
    
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#setStep(Step)
     */
    public void setStep(Step sp){
    	this.step = sp;
    }
    
    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.solution.IteractiveStrategy#getStep()
     */
    public Step getStep(){
    	return step;
    }
}
